package com.example.newdemo.tv;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.newdemo.R;
import com.example.newdemo.tv.adapter.GeneralAdapter;
import com.example.newdemo.tv.adapter.LeftMenuPresenter;
import com.example.newdemo.tv.bridge.RecyclerViewBridge;
import com.example.newdemo.tv.view.FocusableTextView;
import com.example.newdemo.utils.LogUtils;
import com.example.newdemo.view.recyclerView.demo1.DividerItemDecoration;

public class RecyclerTVDemoActivity extends Activity implements RecyclerViewTV.OnItemListener{

    private RecyclerViewBridge mRecyclerViewBridge;
    private View oldView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_recycler_view_demo);
        initView();
    }

    private void initView() {
        RecyclerViewTV recyclerViewTV = findViewById(R.id.recycler_tv_view);



        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerViewTV.setLayoutManager(layoutManager);
        recyclerViewTV.setFocusable(false);

        GeneralAdapter generalAdapter = new GeneralAdapter(new LeftMenuPresenter());
        recyclerViewTV.setAdapter(generalAdapter);

        recyclerViewTV.setAdapter(generalAdapter);


        DividerItemDecoration  mDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
        recyclerViewTV.addItemDecoration(mDecoration);

        recyclerViewTV.setOnItemListener(new RecyclerViewTV.OnItemListener() {
            @Override
            public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {
                // 传入 itemView也可以, 自己保存的 oldView也可以.
//                mRecyclerViewBridge.setUnFocusView(itemView);

                LogUtils.d(" 失去焦点 ** onItemPreSelected   postion = " + position);
                FocusableTextView text = (FocusableTextView) itemView;
                text.setFocus(false);
                ViewCompat.animate(itemView).scaleX(1.0f).scaleY(1.0f).translationZ(1).start();

            }

            @Override
            public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {
//                mRecyclerViewBridge.setFocusView(itemView, 1.5f);
                LogUtils.d(" 获得  -- onItemSelected   postion = " + position);
                FocusableTextView text = (FocusableTextView) itemView;
                text.setFocus(true);
                ViewCompat.animate(itemView).scaleX(1.5f).scaleY(1.5f).translationZ(1).start();
                oldView = itemView;

            }

            /**
             * 这里是调整开头和结尾的移动边框.
             */
            @Override
            public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {
                LogUtils.d("修改  /// onReviseFocusFollow   postion = " + position);
                mRecyclerViewBridge.setFocusView(itemView, 1.5f);
                FocusableTextView text = (FocusableTextView) itemView;
                text.setFocus(true);
                ViewCompat.animate(itemView).scaleX(1.5f).scaleY(1.5f).translationZ(1).start();
                oldView = itemView;
            }
        });

        recyclerViewTV.setOnItemClickListener(new RecyclerViewTV.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerViewTV parent, View itemView, int position) {
                // 测试.
//                mRecyclerViewBridge.setFocusView(itemView, oldView, 1.5f);
                oldView = itemView;
                //
                onViewItemClick(itemView, position);
            }
        });


    }

    private void onViewItemClick(View itemView, int position) {

        LogUtils.d("--点击----- position = " + position);
    }

    @Override
    public void onItemPreSelected(RecyclerViewTV parent, View itemView, int position) {

    }

    @Override
    public void onItemSelected(RecyclerViewTV parent, View itemView, int position) {

    }

    @Override
    public void onReviseFocusFollow(RecyclerViewTV parent, View itemView, int position) {

    }
}
